<?php
/**
 * @title 640. 求解方程 - 统计
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param String $equation
     * @return String
     */
    function solveEquation($equation) {
        /**
         * @since 2022-08-10 拆分左右
         */
        list($left, $right) = explode("=", $equation);

        /**
         * @since 2022-08-10 x数量，数字和
         */
        $cntX = $sumN = 0;

        /**
         * @since 2022-08-10 统计left
         */
        list($c, $s) = self::find($left, true);
        $cntX += $c;
        $sumN += $s;
        // echo "c={$c}, s={$s}", PHP_EOL;

        /**
         * @since 2022-08-10 统计right
         */
        list($c, $s) = self::find($right, false);
        $cntX += $c;
        $sumN += $s;
        // echo "c={$c}, s={$s}", PHP_EOL;

        /**
         * @since 2022-08-10 是否有解
         */
        if($cntX == 0){
            if($sumN == 0){
                /**
                 * @since 2022-08-10 无限解
                 */
                return "Infinite solutions";
            } else {
                /**
                 * @since 2022-08-10 无解
                 */
                return "No solution";
            }
        } else {
            /**
             * @return
             */
            return "x=". ((0-$sumN)/$cntX);
        }
    }

    /**
     * @title 统计
     * @author start2004
     * @since 2022-08-10 Created
     *
     * @param string $string
     * @param boolean $d
     * @return array[int, int]
     */
    function find($string = "", $d = true){
        /**
         * @since 2022-08-10 x数量，数字和
         */
        $cntX = $sumN = 0;

        /**
         * @since 2022-08-10 符号
         */
        if($d){
            $operate = "+";
        } else {
            $operate = "-";
        }

        /**
         * @since 2022-08-10 字符串查找
         */
        $num = "";
        for($i=0; $i<strlen($string); $i++){
            switch ($string[$i]){
                case "+":
                case "-":
                    /**
                     * @since 2022-08-10 处理上一次数据，数字
                     */
                    if($num != ""){
                        if($operate == "+"){
                            $sumN += intval($num);
                        } else {
                            $sumN -= intval($num);
                        }
                        $num = "";
                    } else {}

                    /**
                     * @since 2022-08-10 左边，正向
                     */
                    if($d){
                        $operate = $string[$i];
                    } else {
                        /**
                         * @since 2022-08-10 右边，取反
                         */
                        if($string[$i] == "+"){
                            $operate = "-";
                        } else {
                            $operate = "+";
                        }
                    }
                    break;
                case "x":
                    /**
                     * @since 2022-08-10 空，默认1
                     */
                    if($num == ""){
                        $num = 1;
                    } else {}

                    /**
                     * @since 2022-08-10 处理 x
                     */
                    if($operate == "+"){
                        $cntX += intval($num);
                    } else {
                        $cntX -= intval($num);
                    }

                    /**
                     * @since 2022-08-10 数字置空
                     */
                    $num = "";
                    break;
                default:
                    $num .= $string[$i];
            }

            // echo "string[] = ". $string[$i] . PHP_EOL;
            // echo "cntX = {$cntX}, sumN = {$sumN}, num = {$num}, operate = {$operate}", PHP_EOL;
        }

        /**
         * @since 2022-08-10 收官
         */
        if($num != ""){
            if($operate == "+"){
                $sumN += intval($num);
            } else {
                $sumN -= intval($num);
            }
        } else {}

        /**
         * @return
         */
        return [$cntX, $sumN];
    }
}


/**
 * @url http://127.0.0.1/leetcode/202208/2022.08.10.php
 */
$datas = [
    ["x+5-3+x=6+x-2"], // x=2
    ["x=x"], // Infinite solutions
    ["2x=x"], // x=0
    ["x=x+2"], // No solution
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->solveEquation(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
